package com.lxw.manage_cms.service;

import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsTemplate;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.manage_cms.dao.CmsTemplateDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CmsTemplateServiceImpl implements ICmsTemplateService{

    @Autowired
    CmsTemplateDao cmsTemplateDao;


    @Override
    public List<CmsTemplate> findTemplate(String siteId, String templateName) {
            CmsTemplate cmsTemplate = new CmsTemplate();
            cmsTemplate.setSiteId(siteId);

            ExampleMatcher.GenericPropertyMatcher contains = ExampleMatcher.GenericPropertyMatchers.contains();
            ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("templateName",contains);
            Example<CmsTemplate> example = Example.of(cmsTemplate,exampleMatcher);

            return cmsTemplateDao.findAll(example);
    }

    @Override
    public void addTemplate(CmsTemplate cmsTemplate) {
        if (StringUtils.isEmpty(cmsTemplate.getTemplateName())){
            throw  new RuntimeException("参数不能为空");
        }
        if (StringUtils.isEmpty(cmsTemplate.getSiteId())){
            throw  new RuntimeException("参数不能为空");
        }
        if (StringUtils.isEmpty(cmsTemplate.getTemplateParameter())){
            throw  new RuntimeException("参数不能为空");
        }
        if (StringUtils.isEmpty(cmsTemplate.getTemplateFileId())){
            throw  new RuntimeException("参数不能为空");
        }

        cmsTemplateDao.save(cmsTemplate);
    }
}
